package com.ichi2.anki.pj2;

import android.app.Activity;

import com.ichi2.anki.R;
import com.ichi2.anki.Statistics;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Date;

import androidx.test.espresso.action.ViewActions;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.pressBack;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;


/**
 * Basic tests showcasing simple view matchers and actions like {@link ViewMatchers#withId},
 * {@link ViewActions#click} and {@link ViewActions#typeText}.
 * <p>
 * Note that there is no need to tell Espresso that a view is in a different {@link Activity}.
 * 连接模拟器：nox_adb.exe connect 127.0.0.1:62001
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class StatisticsTest {
    @Rule
    public ActivityScenarioRule<Statistics> activityScenarioRule = new ActivityScenarioRule<>(Statistics.class);


    /**
     * 测试用加载函数
     */
    public void load() {
        onView(withId(R.id.action_time_chooser)).perform(click());
        onView(withText("1 month")).perform(click());

        onView(withId(R.id.action_time_chooser)).perform(click());
        onView(withText("deck life")).perform(click());

        onView(withId(R.id.action_time_chooser)).perform(click());
        onView(withText("1 year")).perform(click());
    }



    /**
     * 切换到所有牌组
     */
    @Test
    public void all_deck() {
        load();
        onView(withId(R.id.dropdown_deck_name)).perform(click());
        pressBack();

        onView(withId(R.id.dropdown_deck_name)).perform(click());
        onView(withText("Cancel")).perform(click());

        onView(withId(R.id.dropdown_deck_name)).perform(click());

        onView(withContentDescription("Filter decks")).perform(click());
        onView(withId(R.id.search_src_text)).perform(click(), typeText("All"));
        onView(withText("All decks")).perform(click());
        onView(withText("All decks")).check(matches(isDisplayed()));
    }


    /**
     * 新增牌组，然后选择
     */
    @Test
    public void add_deck() {
        load();
        String date = String.valueOf(new Date());
        onView(withId(R.id.dropdown_deck_name)).perform(click());
        onView(withContentDescription("Add")).perform(click());
        onView(withId(R.id.md_input_message)).perform(click(), typeText("A" + date));
        onView(withText("OK")).perform(click());
        onView(withText("A" + date)).perform(click());

        onView(withText("A" + date)).perform(click());
        onView(withText("A" + date)).check(matches(isDisplayed()));
    }


    /**
     * 切换显示时间
     */
    @Test
    public void last_time() {
        onView(withId(R.id.action_time_chooser)).perform(click());
        onView(withText("1 month")).perform(click());

        onView(withId(R.id.action_time_chooser)).perform(click());
        onView(withText("deck life")).perform(click());

        onView(withId(R.id.action_time_chooser)).perform(click());
        onView(withText("1 year")).perform(click());

        onView(withId(R.id.action_time_chooser)).perform(click());
        onView(withText("1 year")).check(matches(isDisplayed()));
    }


    /**
     * 点击切换视图
     */
    @Test
    public void click_swipe() {
        load();
        onView(withText("Overview")).perform(click());
        onView(withText("Forecast")).perform(click());
        onView(withText("Review count")).perform(click());
        onView(withText("Review time")).perform(click());
        onView(withText("Intervals")).perform(click());
        onView(withText("Hourly breakdown")).perform(click());
        onView(withText("Weekly breakdown")).perform(click());
        onView(withText("Answer buttons")).perform(click());
        onView(withText("Card types")).perform(click());
        onView(withText("Card types")).check(matches(isDisplayed()));
    }
}
